﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KickerTournament.BL.MonsterDYP
{
    class MonsterDypRound : IRound
    {
        protected List<Player> Players { get; set; }
        protected List<Team> Teams { get; set; }

        public List<Match> Matches { get; set; }

        private IMatchFactory mMatchFactory = new MonsterDypMatchFactory();

        public MonsterDypRound(List<Player> players)
        {
            Players = players;
        }

        public List<Match> generateMatches(IRound roundBefore)
        {
            List<Team> roundTeams = generateTeams();

            Matches = mMatchFactory.createMatches(roundTeams);
            return Matches;
        }

        private List<Team> generateTeams()
        {
            List<Team> teams = new List<Team>();
            List<Player> availablePlayers = Players;

            Random rand = new Random();

            int teamCount = Players.Count / 4;
            for (int teamIndex = 0; teamIndex < teamCount; teamIndex++)
            {
                int player1Index = rand.Next(availablePlayers.Count -1);
                Player player1 = availablePlayers[player1Index];
                availablePlayers.Remove(player1);

                int player2Index = rand.Next(availablePlayers.Count -1);
                Player player2 = availablePlayers[player1Index];
                availablePlayers.Remove(player2);

                Team createdTeam = new Team(player1, player2);
                teams.Add(createdTeam);
            }

            return teams;
        }
    }
}
